package com.game;

/**
 * This program is written by Will Woo(WuWei)
 * 
 * @author Will Woo(fifyrio@qq.com) all rights reserved.
 */
public class ShowHand
{
	private static int CARD_NUM = 28;
	private static int PLAYER = 5;
	private static int PLAYER_CARD = 5;
	private static String[] NAME = { "JackieChen  ", "BillGates   ",
			"SteveJobs   ", "LebronJames ", "WillWoo     " };

	private Card[] card = new Card[CARD_NUM];
	private static Player[] player = new Player[PLAYER];
	private static Player[] survivor = new Player[PLAYER];
	private String[] CardType = { "\3", "\4", "\5", "\6" };
	private String[] CardNum = { "8", "9", "10", "J", "Q", "K", "A" };

	/**
	 * 内部类描述玩家
	 */
	public class Player
	{
		private String name;
		private boolean status;
		private Card[] playerCard = new Card[PLAYER_CARD];
		public int[] bonus = { 1000, 1000 };

		public Player()
		{
		}

		public Player(String name, boolean s, Card[] pc)
		{
			this.name = name;
			this.status = s;
			this.playerCard = pc;
		}

		public String getName()
		{
			return this.name;
		}

		public boolean getStatus()
		{
			return this.status;
		}

		public void setStatus(boolean s)
		{
			this.status = s;
		}

		public void addCard(Card card, int i)
		{
			this.playerCard[i - 1] = card;
		}

	}

	/**
	 * 内部类描述纸牌
	 */
	public class Card
	{
		private int type;
		private int num;
		public int count = 0;

		public Card(int t, int n)
		{
			this.type = t;
			this.num = n;
		}

	}

	/*
	 * 方法：洗牌
	 */
	public void shuffle()
	{
		for (int i = 0; i < 4; i++)
			for (int j = 0; j < 7; j++)
				card[7 * i + j] = new Card(i, j + 8);
		Card buf;
		java.util.Random random = new java.util.Random();
		for (int i = 0; i < 200; i++)
		{
			int n1 = random.nextInt(28);
			int n2 = random.nextInt(28);
			buf = card[n1];
			card[n1] = card[n2];
			card[n2] = buf;
		}

		// int lastNum=CARD_NUM;
	}

	/*
	 * 方法：发牌
	 */
	public void deal(Player[] p, int shuffleNum)
	{
		if (shuffleNum == 1 || shuffleNum == 2)
		{
			int lastNum = CARD_NUM - 1;
			while (card[lastNum--] == null)
				;
			lastNum++;
			int location = 0;
			for (int i = 0; i < 5; i++)
			{
				p[location++].addCard(card[lastNum], shuffleNum);
				card[lastNum--] = null;
			}
		}
		else
		{
			int i = CARD_NUM - 1;
			while (card[i--] == null)
				;
			i++;
			for (int j = 0; j < p.length && p[j] != null; j++)
			{
				p[j].addCard(card[i], shuffleNum);
				card[i--] = null;
			}
		}
	}

	/*
	 * 方法：创建玩家
	 */
	public void create()
	{
		for (int i = 0; i < PLAYER; i++)
			player[i] = new Player(NAME[i], true, new Card[5]);
	}

	/*
	 * 方法：确定剩下的玩家
	 */
	public void findSur()
	{
		int j = 0;
		for (int k = 0; k < 5; k++)
			survivor[k] = null;
		for (int i = 0; i < 5; i++)
		{

			if (player[i].getStatus() == true)
				survivor[j++] = player[i];
		}
	}

	/*
	 * 方法：显示所有牌
	 */
	public void printCard()
	{
		for (int i = 0; i < 28; i++)
		{
			System.out.print(this.card[i].type + " ");
			System.out.println(this.card[i].num);
		}
	}

	/*
	 * 方法：显示玩家的牌
	 */
	public void printPCard(int r)
	{
		for (int i = 0; i < survivor.length && survivor[i] != null; i++)
			System.out.print(survivor[i].getName());
		System.out.print("跟！");
		System.out.print("\n");
		System.out.println("发第" + (r) + "张牌。。。。。");
		System.out.println("发完后");
		for (int i = 0; i < survivor.length && survivor[i] != null; i++)
		{
			System.out.print(survivor[i].getName() + ":" + "xx  ");
			for (int j = 1; j < r; j++)
				System.out.print(convertType(survivor[i].playerCard[j].type)
						+ convertNum(survivor[i].playerCard[j].num) + " ");
			System.out.print("\n");
		}
		System.out.print("\n");
	}

	/*
	 * 方法：显示玩家底牌
	 */
	public void printAce()
	{
		for (int i = 0; i < survivor.length && survivor[i] != null; i++)
		{
			System.out.print(survivor[i].getName() + ":");
			for (int j = 0; j < 5; j++)
				System.out.print(convertType(survivor[i].playerCard[j].type)
						+ convertNum(survivor[i].playerCard[j].num) + " ");
			System.out.print("\n");
		}
	}

	/*
	 * 方法：随即产生跟随的玩家
	 */
	public void follow(Player[] p)
	{
		int num = 0;
		while (num < p.length && p[num++] != null)
			;
		if (num != 5 || p[4] == null)
			num--;
		switch (num)
		// 判断现有玩家是否超过2人
		{
		case 1:
			System.out.println("杯具，重开一局");
			return;
		case 2:
			return;
		case 3:
		case 4:
		case 5:
			java.util.Random random = new java.util.Random();
			int quit = random.nextInt(num);
			p[quit].setStatus(random.nextBoolean());
			break;
		default:
			System.out.println("BUG!");
		}
	}

	/*
	 * 方法：数字转换成字符串
	 */
	public String convertType(int type)
	{
		switch (type)
		{
		case 0:
			return CardType[0];
		case 1:
			return CardType[1];
		case 2:
			return CardType[2];
		case 3:
			return CardType[3];
		default:
			return null;
		}
	}

	public String convertNum(int num)
	{
		switch (num)
		{
		case 8:
			return CardNum[0];
		case 9:
			return CardNum[1];
		case 10:
			return CardNum[2];
		case 11:
			return CardNum[3];
		case 12:
			return CardNum[4];
		case 13:
			return CardNum[5];
		case 14:
			return CardNum[6];
		default:
			return null;
		}
	}

	public static void main(String[] args)
	{
		System.out.println("Welcome to ShowHand!");
		ShowHand SH = new ShowHand();
		SH.create();
		SH.shuffle();
		// SH.printCard();
		int round = 1;
		System.out.println("发两张底牌牌");
		SH.deal(player, round++);
		SH.deal(player, round++);

		for (int i = 0; i < 5; i++)
		{
			System.out.println(SH.player[i].getName() + ":" + "xx  "
					+ SH.convertType(SH.player[i].playerCard[1].type)
					+ SH.convertNum(SH.player[i].playerCard[1].num));
		}
		System.out.print("\n");

		SH.findSur();
		while (round != 6)
		{
			System.out.println("跟或不跟？");
			SH.follow(survivor);
			SH.findSur();
			SH.deal(survivor, round++);
			SH.printPCard(round - 1);
		}
		System.out.println("终极对决！开。。。底。。。牌。。。");
		SH.printAce();
		System.out.println("GameOver");
	}
}
